<?php

include 'config.php';

if(isset($_GET['server'])){$server = $_GET['server'];}else{$server = null;}
if(isset($_GET['range'])){$range = $_GET['range'];}else{$range = null;}
if(isset($_GET['stat'])){$stat = $_GET['stat'];}else{$stat = null;}
if(isset($_GET['page'])){$page = $_GET['page'];}else{$page = 0;}

/*SET WHICH SERVER TO PULL STATS FROM*/
if($server=="All"){
	$server_string = " ";
}else{
	$server_string = "where vc_server_name = '".$server."' ";
}

/*SET WHICH TIME FRAME TO PULL STATS FOR*/
$record_string = "user_all_record";
$stat_string = "user_all_stats";
if($range=="month"){
	$record_string = "user_month_record";
	$stat_string = "user_month_stats";	
}
if($range=="week"){
	$record_string = "user_week_record";
	$stat_string = "user_week_stats";
}
$table_string= $record_string." r on r.bi_user_id = p.bi_user_id join ".$stat_string." s on s.bi_user_id = p.bi_user_id ";

/* SET STAT PART OF QUERY HERE*/
switch($stat){
	case "winpercent":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, (i_wins/i_games) ";
	$order_string = " order by (i_wins/i_games) desc";
	$stat_header = "Win %";
	break;
	case "wins":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, i_wins ";
	$order_string = " order by i_wins desc";
	$stat_header = "Wins";
	break;
	case "losses":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, i_losses ";
	$order_string = " order by i_losses desc";
	$stat_header = "Losses";
	break;
	case "games":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, i_games ";
	$order_string = " order by i_games desc";
	$stat_header = "Games";
	break;
	case "tags":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, i_tags ";
	$order_string = " order by i_tags desc";
	$stat_header = "Tags";
	break;
	case "returns":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, i_returns ";
	$order_string = " order by i_returns desc";
	$stat_header = "Returns";
	break;
	case "captures":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, i_captures ";
	$order_string = " order by i_captures desc";
	$stat_header = "Captures";
	break;
	case "grabs":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, i_grabs ";
	$order_string = " order by i_grabs desc";
	$stat_header = "Grabs";
	break;
	case "drops":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, i_drops ";
	$order_string = " order by i_drops desc";
	$stat_header = "Drops";
	break;
	case "popped":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, i_popped ";
	$order_string = " order by i_popped desc";
	$stat_header = "Popped";
	break;
	case "hours":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, f_hours ";
	$order_string = " order by f_hours desc";
	$stat_header = "Hours";
	break;
	case "dcs":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, i_dcs ";
	$order_string = " order by i_dcs desc";
	$stat_header = "Disconnects";
	break;
	case "winpercentnodcs":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, (i_wins/(i_games-i_dcs)) ";
	$order_string = " order by (i_wins/(i_games-i_dcs)) desc";
	$stat_header = "Win % (DCs excluded)";
	break;
	case "tagpop":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, (i_tags/i_popped) ";
	$order_string = " order by (i_tags/i_popped) desc";
	$stat_header = "Tag/Pop Ratio";
	break;
	case "taggame":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, (i_tags/i_games) ";
	$order_string = " order by (i_tags/i_games) desc";
	$stat_header = "Tags Per Game";
	break;
	case "taghour":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, (i_tags/f_hours) ";
	$order_string = " order by (i_tags/f_hours) desc";
	$stat_header = "Tags Per Hour";
	break;
	case "capgame":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, (i_captures/i_games) ";
	$order_string = " order by (i_captures/i_games) desc";
	$stat_header = "Captures Per Game";
	break;
	case "caphour":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, (i_captures/f_hours) ";
	$order_string = " order by (i_captures/f_hours) desc";
	$stat_header = "Captures Per Hour";
	break;
	case "returngame":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, (i_returns/i_games) ";
	$order_string = " order by (i_returns/i_games) desc";
	$stat_header = "Returns Per Game";
	break;
	case "returnhour":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, (i_returns/f_hours) ";
	$order_string = " order by (i_returns/f_hours) desc";
	$stat_header = "Returns Per Hour";
	break;
	case "popgame":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, (i_popped/i_games) ";
	$order_string = " order by (i_popped/i_games) desc";
	$stat_header = "Popped Per Game";
	break;
	case "pophour":
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, (i_popped/f_hours) ";
	$order_string = " order by (i_popped/f_hours) desc";
	$stat_header = "Popped Per Hour";
	break;
										
	default:
	$select_string = "select p.vc_server_name, r.dt_last_update, r.vc_name, i_wins ";
	$order_string = " order by i_wins desc";
	$stat_header = "Wins";
	break;
}

/* PAGINATION */
$records_per_page = 50;
$start = $page * $records_per_page;
$sql = "select bi_user_id from user_profile";
$result = mysqli_query($con, $sql);
$total_records = mysqli_num_rows($result);
$total_pages = ceil($total_records/$records_per_page);
$actual_page = $page + 1;

/*RUN THE QUERY AND GENERATE THE TABLE*/
if($server!=null && $range!=null && $stat!=null){
	
	$sql = $select_string.", vc_profile_string "
	."from user_profile p join "
	.$table_string
	.$server_string
	.$order_string
	." limit ".$start.",".$records_per_page;
	$result = mysqli_query($con, $sql);

	$rank = 1 + $start;
	echo "<table class='leaderTable'>";
	echo "<tr><th>Rank</th><th>Server</th><th>Last Updated</th><th>Name</th><th>".$stat_header."</th>";
	while($row=mysqli_fetch_array($result, MYSQLI_NUM)){
		echo "<tr><td class='leaderTable'>".$rank
		."</td><td class='leaderTable'>".$row[0]
		."</td><td class='leaderTable'>".$row[1]
		."</td><td class='leaderTable'><a href='http://tagpro-".$row[0].".koalabeast.com/profile/".$row[4]."'>".$row[2]."</a>"
		."</td><td class='leaderTable'>".$row[3]."</td></tr>";
		$rank++;
	}
	echo "</table>";
/* Pagination UI */	

	echo "<table><tr>";
	if($actual_page==1){
		echo "<td> <button disabled>Previous</button> </td>";
	}else{
		echo "<td> <button onclick='getChart(".($page-1).")'>Previous</button> </td>";
	}
	echo "<td> Page ".$actual_page." of ".$total_pages."</td>";
	if($actual_page==$total_pages){
		echo "<td> <button disabled>Next</button> </td>";
	}else{
		echo "<td> <button onclick='getChart(".($page+1).")'>Next</button> </td>";
	}
	echo "</tr></table>";
	
}else{
	echo "Error Getting Table. You broke it!";
}



mysqli_close($con);
?>